﻿using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Extensions.Configuration.Database.Sample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var hostBuilder = Host.CreateDefaultBuilder(args)
                .ConfigureServices(services =>
                {
                    // 添加测试服务
                    services.AddHostedService<ConfigurationBackgroundService>();
                }).ConfigureAppConfiguration(
                    configure =>
                    {
                        configure.Sources.Clear();

                        configure.AddDatabase(options =>
                        {
                            // 需要提前在数据库中写入数据
                            options.UseSqlite(
                                $"DataSource={System.IO.Path.Combine(Environment.CurrentDirectory, "sample.db")}");
                        }, reloadOnChange: true);

                        // configure.AddJsonFile("appsettings.json", false, true);
                    }).ConfigureLogging(builder => builder.AddConsole());

            var host = hostBuilder.Build();

            await host.RunAsync();
        }
    }
}